package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IProdInstService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CheckWayHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    public IProdInstService prodInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {

        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);

        KiteProdInst prodInst = querySceneInstService.listProdInstsFromOfferInst(custDTO1.getCustId(), kiteSceneInst.getRootInstId()).stream()
            .filter(inst -> !KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.X.name(), ActionType.D.name()))
            .findFirst().orElse(null);
        String mainServiceOfferId = kiteSceneInst.getServiceOfferId();
        KiteAttr checkTypeAttr = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, commonAttr.getFieldName()).getResultObject();

        // 国际互联网，不展示核查方式，默认传：2-快速并行核查
        String offerId = querySceneInstService.getOfferInst(kiteSceneInst.getCustId(), kiteSceneInst.getRootInstId()).getOfferId();
        if(KiteStringUtils.isEqual(offerId, "50040853")) {
            commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            commonAttr.setValue("2");
            commonAttr.setValueDesc("快速并行核查");
        } else
        // 资源核查
        if (KiteStringUtils.isEqual(mainServiceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)) {
            if (LocalAttrUtils.isCrossDomain(kiteSceneInst, prodInst)) {
                // 跨域，不包含异地发起的本地业务，即租用范围为本地区内或本地区间的情况
                commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                commonAttr.setValue("2");
                commonAttr.setValueDesc("快速并行核查");
            } else {
                commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                String valueDesc = Optional.ofNullable(AttrValueCache.getAttrValue(checkTypeAttr.getAttrId(), checkTypeAttr.getValue())).map(AttrValue::getAttrValueName).orElse(null);
                commonAttr.setValue(valueDesc == null ? null : checkTypeAttr.getValue());
                checkTypeAttr.setValueDesc(valueDesc);
            }
        }

        // 订购、变更
        if(KiteStringUtils.equalsAny(mainServiceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER, ServiceOfferConsts.SERVICE_OFFER_MODIFY)) {
            if (LocalAttrUtils.isCrossDomain(kiteSceneInst, prodInst)) {
                // 跨域，含异地发起的本地业务，即租用范围为本地区内或本地区间的情况
                KiteAttr kiteAttr = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10005").getResultObject();
                if (null != kiteAttr && KiteStringUtils.equalsAny(kiteAttr.getValue(),"2","1")) {
                    commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    if(LocalAttrUtils.LOCAL_PROD.contains(prodInst.getProdId())) {
                        commonAttr.setValue("1");
                        commonAttr.setValueDesc("精准串行核查");
                    } else {
                        commonAttr.setValue("2");
                        commonAttr.setValueDesc("快速并行核查");
                    }
                } else {
                    commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                }
            } else {
                commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            }
        }

        checkTypeAttr.setPreValue(checkTypeAttr.getValue());
        checkTypeAttr.setShowType(commonAttr.getShowType());
        checkTypeAttr.setValue(commonAttr.getValue());
        checkTypeAttr.setValueDesc(commonAttr.getValueDesc());
        operateSceneCommonService.setCommonAttr(sceneInstId, checkTypeAttr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
//        // 订单信息（资源核查） 资源预占期
//        KiteAttr orderResourceAttr = querySceneInstService
//            .getCommonAttrsByFieldName(sceneInstId, "ORD10013", "204");
//        if (Objects.isNull(orderResourceAttr)) {
//            return super.apply(sceneInstId, fieldName, value, valueDesc);
//        }
//        // 当资源是否预占选择是的展示且必填，否则不展示
//        if (KiteStringUtils.equals("1", value)) {
//            orderResourceAttr.setShowType("D");
//            orderResourceAttr.setIsNull("F");
//        } else {
//            orderResourceAttr.setShowType("H");
//            orderResourceAttr.setIsNull("T");
//        }
//        operateSceneCommonService.setCommonAttr(sceneInstId, orderResourceAttr);
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
